#!/usr/bin/env python3
"""
Nuitka 编译脚本
"""
import subprocess
import sys
import os

def build_executable():
    """使用 Nuitka 编译可执行文件"""
    
    # Nuitka 编译命令
    cmd = [
        sys.executable, "-m", "nuitka",
        "--standalone",
        "--onefile",
        "--windows-console-mode=disable",  # 隐藏控制台窗口
        "--windows-icon-from-ico=ssh.ico",  # 设置图标
        "--include-data-dir=logs=logs",  # 包含日志目录
        "--include-data-file=config.ini=config.ini",  # 包含配置文件
        "--include-package=modules",  # 包含模块包
        "--follow-imports",
        "--assume-yes-for-downloads",
        # 排除可能有问题的模块
        "--nofollow-import-to=numpy",
        "--nofollow-import-to=scipy",
        "--nofollow-import-to=matplotlib",
        # 优化选项
        "--lto=no",  # 禁用链接时优化，减少编译时间
        "--jobs=2",  # 限制并行作业数
        "ssh_tool_modular.py"
    ]
    
    print("开始编译...")
    print("命令:", " ".join(cmd))
    
    try:
        result = subprocess.run(cmd, check=True, capture_output=True, text=True)
        print("编译成功!")
        print(result.stdout)
    except subprocess.CalledProcessError as e:
        print("编译失败:")
        print("错误输出:", e.stderr)
        print("标准输出:", e.stdout)
        return False
    
    return True

if __name__ == "__main__":
    success = build_executable()
    if success:
        print("\n编译完成! 可执行文件位于 dist/ 目录")
    else:
        print("\n编译失败，请检查错误信息")
        sys.exit(1)